package com.hyt.it.ogt.ks.service.impl;

import java.time.LocalDateTime;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hyt.it.ogt.ks.config.ConfigManager;
import com.hyt.it.ogt.ks.model.entity.CandidateProcess;
import com.hyt.it.ogt.ks.model.vo.ValidPhotoVO;
import com.hyt.it.ogt.ks.mq.FaceValidResultPhotoDownloadMqProducer;
import com.hyt.it.ogt.ks.mq.model.FaceValidResultPhotoDownloadDTO;
import com.hyt.it.ogt.ks.service.ICandidateProcessService;
import com.hyt.it.ogt.ks.service.ILogService;
import org.springframework.stereotype.Service;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.utils.uuid.UUIDUtils;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.enums.CandidateLogOperateType;
import com.hyt.it.ogt.ks.feign.KwClient;
import com.hyt.it.ogt.ks.feign.model.SecondValidFaceDTO;
import com.hyt.it.ogt.ks.mapper.CandidateLogMapper;
import com.hyt.it.ogt.ks.model.entity.CandidateLog;
import com.hyt.it.ogt.ks.service.IPcMonitorService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wuyingge
 * @version 1.0.0
 * @ClassName PcMonitorServiceServiceImpl.java
 * @Description PC端视频监控接口
 * @createTime 2022年09月27日 16:41:00
 */
@Slf4j
@Service
public class PcMonitorServiceServiceImpl implements IPcMonitorService {
    @Resource
    private CandidateLogMapper candidateLogMapper;
    @Resource
    private KwClient kwClient;
    @Resource
    private ILogService iLogService;
    @Resource
    private FaceValidResultPhotoDownloadMqProducer faceValidResultPhotoDownloadMqProducer;
    @Resource
    private ConfigManager configManager;
    @Resource
    private ICandidateProcessService iCandidateProcessService;

    /**
     * pc二次登录人脸验证身
     *
     * @param candidateId
     * @param imageUrl
     * @param examId
     * @param request
     * @return
     */
    @Override
    public Boolean secondValidFace(String candidateId, String imageUrl, String examId, HttpServletRequest request){
        String res = kwClient.secondValidFace(candidateId, imageUrl, examId);
        ApiResponse<SecondValidFaceDTO> response = ApiResponse.ConvertRet(res,SecondValidFaceDTO.class);
        if(Objects.nonNull(response) && BizStatusCode.KW_SUCCESS_OK.getCode().equals(response.getCode())
                && Objects.nonNull(response.getData())){
            //事件入库
            saveSecondValidFaceLog(examId,candidateId,response.getData(),request);
            if(Objects.equals(Boolean.TRUE,response.getData().getIsPassed())){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    //事件入库
    private void saveSecondValidFaceLog(String examId,String candidateId,SecondValidFaceDTO result, HttpServletRequest request){
        CandidateLog candidateLog = new CandidateLog();
        candidateLog.setId(UUIDUtils.getUUID());
        candidateLog.setCandidateId(candidateId);
        candidateLog.setExamId(examId);
        candidateLog.setIp(iLogService.getIp(request));
        candidateLog.setOperateType(CandidateLogOperateType.SECOND_FACE_MATCH_LOGIN.getCode());
        String content = "";

        if(Objects.equals(Boolean.TRUE,result.getIsPassed()) && Objects.equals(3,result.getPassType())){
            content = "二次登录人脸核验对比认证，查询考试照片信息表没有记录";
        } else if(Objects.equals(Boolean.TRUE,result.getIsPassed()) && Objects.equals(1,result.getPassType())){
            content = "二次登录人脸核验对比认证，设置过手动跳过人脸认证，不再进行认证";
        } else if(Objects.equals(Boolean.TRUE,result.getIsPassed()) && Objects.equals(2,result.getPassType())){
            content = "二次登录人脸核验对比认证,考生为港澳台人员";
        } else if(Objects.equals(Boolean.FALSE,result.getIsPassed())){
            content = "二次登录人脸核验对比认证结果不匹配";
        } else if (Objects.equals(Boolean.TRUE,result.getIsPassed())){
            content = "二次登录人脸核验对比认证结果匹配";
        }
        candidateLog.setOperateContent(content);
        candidateLog.setBeginTime(LocalDateTime.now());
        candidateLogMapper.insert(candidateLog);
    }

    @Override
    public void sendMessageAndLog(ValidPhotoVO validPhotoVO) {
        faceValidResultPhotoDownloadMqProducer.sendMessage(FaceValidResultPhotoDownloadDTO.builder()
                .path(configManager.getPhotoBucketPath() + validPhotoVO.getExamId() + "/" + validPhotoVO.getAdmissionNumber() + "/")
                .officeId("0").examCandidateId(validPhotoVO.getExamCandidateId()).orderId(validPhotoVO.getOrderId()).build());
        iCandidateProcessService.lambdaUpdate().eq(CandidateProcess::getExamId, validPhotoVO.getExamId())
                .eq(CandidateProcess::getCandidateId, validPhotoVO.getExamCandidateId())
                .set(CandidateProcess::getCheckedIdCard, true).update();
        kwClient.updateCandidateFaceState(validPhotoVO.getExamCandidateId());
        // 轨迹
        iLogService.faceLog(validPhotoVO.getExamId(), validPhotoVO.getExamCandidateId(), validPhotoVO.getOrderId());
    }

}
